//
//  PassWordView.m
//  密码输入框
//
//  Created by zhouLiHuan on 2017/2/8.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "PassWordView.h"

@implementation PassWordView

- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title{
    self = [super initWithFrame:frame];
    if (self) {
        //标题
        _label_title = [[UILabel alloc] init];
        _label_title.frame = CGRectMake(0, 20, SCREEN_WIDTH, 20);
        _label_title.text = title;
        _label_title.textAlignment = NSTextAlignmentCenter;
        _label_title.textColor = [UIColor grayColor];
        [self addSubview:_label_title];
    
    //textFiled
        _textField = [[UITextField alloc] init];
        _textField.frame = CGRectMake(0, 0, 0, 0);
        _textField.delegate = self;
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        [_textField addTarget:self action:@selector(textFiledDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_textField];

        //假的输入框
        _viewBox1 = [self creatFalseTextFiledWithFrame:CGRectMake(10, 60, boxWidth, boxWidth)];
        [self addSubview:_viewBox1];
        _viewBox2 = [self creatFalseTextFiledWithFrame:CGRectMake(20+boxWidth*1, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
        [self addSubview:_viewBox2];
        _viewBox3 = [self creatFalseTextFiledWithFrame:CGRectMake(30+boxWidth*2, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
        [self addSubview:_viewBox3];
        _viewBox4 = [self creatFalseTextFiledWithFrame:CGRectMake(40+boxWidth*3, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
        [self addSubview:_viewBox4];
        _viewBox5 = [self creatFalseTextFiledWithFrame:CGRectMake(50+boxWidth*4, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
        [self addSubview:_viewBox5];
        _viewBox6 = [self creatFalseTextFiledWithFrame:CGRectMake(60+boxWidth*5, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
        [self addSubview:_viewBox6];
        
        //密码点
        _labelPoint1 = [self creatPasswordPointWithSuperView:_viewBox1];
        _labelPoint2 = [self creatPasswordPointWithSuperView:_viewBox2];
        _labelPoint3 = [self creatPasswordPointWithSuperView:_viewBox3];
        _labelPoint4 = [self creatPasswordPointWithSuperView:_viewBox4];
        _labelPoint5 = [self creatPasswordPointWithSuperView:_viewBox5];
        _labelPoint6 = [self creatPasswordPointWithSuperView:_viewBox6];
        
    }
    
    return self;
}

//创建假的输入框
- (UIView *)creatFalseTextFiledWithFrame:(CGRect)frame{
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    [view.layer setBorderWidth:1.0];
    view.layer.borderColor = [[UIColor grayColor] CGColor];
    return view;
}

#pragma mark - 创建密码点的方法
- (UILabel *)creatPasswordPointWithSuperView:(UIView *)superView{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((superView.frame.size.width - 10)*0.5, (superView.frame.size.width - 10)*0.5, 10, 10)];
    [label.layer setCornerRadius:5.0];
    [label.layer setMasksToBounds:YES];
    [superView addSubview:label];
    label.backgroundColor = [UIColor blackColor];
    label.hidden = YES;
    return label;
}


#pragma mark - textFiled事件
- (void)textFiledDidChange:(UITextField *)textFiled{
    switch (textFiled.text.length) {
        case 0:
            _labelPoint1.hidden = YES;
            _labelPoint2.hidden = YES;
            _labelPoint3.hidden = YES;
            _labelPoint4.hidden = YES;
            _labelPoint5.hidden = YES;
            _labelPoint6.hidden = YES;
            break;
            case 1:
            _labelPoint1.hidden = NO;
            _labelPoint2.hidden = YES;
            _labelPoint3.hidden = YES;
            _labelPoint4.hidden = YES;
            _labelPoint5.hidden = YES;
            _labelPoint6.hidden = YES;
            break;
            case 2:
            _labelPoint1.hidden = NO;
            _labelPoint2.hidden = NO;
            _labelPoint3.hidden = YES;
            _labelPoint4.hidden = YES;
            _labelPoint5.hidden = YES;
            _labelPoint6.hidden = YES;
            break;
            case 3:
            _labelPoint1.hidden = NO;
            _labelPoint2.hidden = NO;
            _labelPoint3.hidden = NO;
            _labelPoint4.hidden = YES;
            _labelPoint5.hidden = YES;
            _labelPoint6.hidden = YES;
            break;
            case 4:
            _labelPoint1.hidden = NO;
            _labelPoint2.hidden = NO;
            _labelPoint3.hidden = NO;
            _labelPoint4.hidden = NO;
            _labelPoint5.hidden = YES;
            _labelPoint6.hidden = YES;
            break;
            case 5:
            _labelPoint1.hidden = NO;
            _labelPoint2.hidden = NO;
            _labelPoint3.hidden = NO;
            _labelPoint4.hidden = NO;
            _labelPoint5.hidden = NO;
            _labelPoint6.hidden = YES;
            break;
            case 6:
            _labelPoint1.hidden = NO;
            _labelPoint2.hidden = NO;
            _labelPoint3.hidden = NO;
            _labelPoint4.hidden = NO;
            _labelPoint5.hidden = NO;
            _labelPoint6.hidden = NO;
            break;
            
        default:
            break;
    }
    
    if (textFiled.text.length == 6) {
        if ([self.delegate respondsToSelector:@selector(passwordView:withPasswordString:)]) {
            [self.delegate passwordView:self withPasswordString:textFiled.text];
        }
    }
    
}



@end
